package com.shj.web.controller;

import com.shj.model.PageRequest;
import com.shj.model.PageResult;
import com.shj.model.SysSubFree;
import com.shj.model.SysSubject;
import com.shj.service.SysSubFreeService;
import com.shj.service.SysSubjectService;
import com.shj.util.FileUtil;
import org.apache.poi.hwpf.HWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * (SysSubject)表控制层
 *
 * @author makejava
 * @since 2020-03-12 12:49:36
 */
@Controller
@RequestMapping("/subject")
public class SysSubjectController {
    /**
     * 服务对象
     */
    @Resource
    private SysSubjectService sysSubjectService;
    @Resource
    private SysSubFreeService sysSubFreeService;

    /**
     * @Description 管理员查看所有项目
     * @author songhuajian
     * @date 2020/6/6 17:11
     */
    @GetMapping("/admin/list")
    @ResponseBody
    public PageResult selectPage(PageRequest pageRequest, String leaderId) {
        return this.sysSubjectService.findPage(pageRequest, leaderId);
    }

    @GetMapping("/one")
    @ResponseBody
    public SysSubject findOne(String subId) {
        SysSubject sysSubject = sysSubjectService.findBySubId(subId);
        SysSubFree sysSubFree = sysSubFreeService.queryBySubId(subId, 0);
        sysSubject.setFree(sysSubFree);
        return sysSubject;
    }

    /**
     * @Description 负责人查询自己的项目
     * @author songhuajian
     * @date 2020/6/6 17:10
     */
    @GetMapping("/leader/list")
    @ResponseBody
    public PageResult findByLeaderId(PageRequest pageRequest, String leaderId) {
        return this.sysSubjectService.findPage(pageRequest, leaderId);
    }

    /**
     * @Description 负责人查询立项通过的项目
     * @author songhuajian
     * @date 2020/6/24 22:02
     */
    @GetMapping("/leader/forConclusion")
    @ResponseBody
    public PageResult forConclusion(PageRequest pageRequest, String leaderId) {
        return sysSubjectService.findForConlusion(pageRequest, leaderId);
    }

    /**
     * @Description 添加立项项目
     * @author songhuajian
     * @date 2020/6/6 17:10
     */
    @PostMapping("/leader/add")
    @ResponseBody
    public SysSubject add(MultipartFile file, SysSubject sysSubject) throws IOException {
        // 保存文件获取保存路径
        String url = FileUtil.mvcFileUpload(file);
        sysSubject.setMatchFile(url);
        return sysSubjectService.insert(sysSubject);
    }

    /**
     * @Description 下载立项申请书并赋值
     * @author songhuajian
     * @date 2020/6/6 18:56
     */
    @GetMapping("/leader/download")
    public void downloadFile(String subId, HttpServletResponse response) throws IOException {
        HWPFDocument doc = sysSubjectService.downloadFile(subId);
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        //设置http响应头
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode("立项申请书", "UTF-8") + ".doc");
        //输出流
        OutputStream outputStream = response.getOutputStream();
        doc.write(outputStream);
        outputStream.close();
    }

    /**
     * @Description 上传立项申请书
     * @author songhuajian
     * @date 2020/6/6 21:45
     */
    @PostMapping("/leader/upload")
    @ResponseBody
    public void uploadFile(MultipartFile file, String subId) throws IOException {
        // 保存文件获取保存路径
        String url = FileUtil.mvcFileUpload(file);
        SysSubject sysSubject = sysSubjectService.findBySubId(subId);
        sysSubject.setSubFile(url);
        sysSubjectService.update(sysSubject);
    }

    /**
     * @Description 审核立项项目
     * @author songhuajian
     * @date 2020/6/8 14:59
     */
    @ResponseBody
    @PostMapping("/admin/review")
    public SysSubject review(@RequestBody SysSubject sysSubject) {
        SysSubject subject = sysSubjectService.findBySubId(sysSubject.getSubId());
        subject.setOpinion(sysSubject.getOpinion());
        subject.setSubStatus(sysSubject.getSubStatus());
        sysSubjectService.update(subject);
        return subject;
    }

    /**
     * @Description 删除项目
     * @author songhuajian
     * @date 2020/6/7 15:40
     */
    @DeleteMapping("/leader/delete")
    @ResponseBody
    public void delete(String subId) {
        sysSubjectService.deleteBySubId(subId);
    }
}